<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="clientid">
        <el-input
          v-model="queryParams.clientid"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生" prop="studentid">
        <el-input
          v-model="queryParams.studentid"
          placeholder="请输入学生"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校" prop="deptid" v-hasRole="['admin', 'common']">
        <el-input
          v-model="queryParams.deptid"
          placeholder="请输入学校ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体测项目" prop="projectid">
        <el-input
          v-model="queryParams.projectid"
          placeholder="请输入体测项目编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="sfz">
        <el-input
          v-model="queryParams.sfz"
          placeholder="请输入证件号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
	  
<!-- 	  
	
	
	<el-form-item label="订阅" prop="topic">
	  <el-input
	    v-model="queryParams.topic"
	    placeholder="请输入订阅"
	    clearable
	    @keyup.enter="handleQuery"
	  />
	</el-form-item>
	
	<el-form-item label="体测教师" prop="teacher">
	  <el-input
	    v-model="queryParams.teacher"
	    placeholder="请输入体测教师"
	    clearable
	    @keyup.enter="handleQuery"
	  />
	</el-form-item>
	  <el-form-item label="1未读 2已读" prop="isread">
	    <el-input
	      v-model="queryParams.isread"
	      placeholder="请输入1未读 2已读"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item>	
      <el-form-item label="${comment}" prop="projecttypeid">
        <el-input
          v-model="queryParams.projecttypeid"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="classid">
        <el-input
          v-model="queryParams.classid"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="classnj">
        <el-input
          v-model="queryParams.classnj"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
	  -->
	  <el-row style="justify-content: right;">
		  <el-form-item>
		    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
		    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
		  </el-form-item>
	  </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tc:tcdevicedata:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tc:tcdevicedata:remove']"
        >删除</el-button>
      </el-col>
	  <!-- 
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tc:tcdevicedata:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tc:tcdevicedata:export']"
        >导出</el-button>
      </el-col>
	  
	   -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :show-overflow-tooltip="true" v-loading="loading" :data="tcdevicedataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="编号" align="center" prop="id" width="0" />
	  <!-- A体测 B娱乐 -->
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="设备" align="center" prop="clientid" />
      <el-table-column label="证件号" align="center" prop="sfz" />
      <!-- <el-table-column label="学生" align="center" prop="studentid" /> -->
      <el-table-column label="性别" align="center" prop="sex">
	    <template #default="scope">
	      <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
	    </template>
	  </el-table-column>
      <el-table-column label="年级" align="center" prop="classnj" >
        <template #default="scope">
          <dict-tag :options="tc_nj" :value="scope.row.classnj"/>
        </template>
      </el-table-column>
      <el-table-column label="体测教师" align="center" prop="teacher" />
      <el-table-column label="学校" align="center" prop="deptid" v-hasRole="['admin', 'common']" />
      <el-table-column label="体测项目" align="center" prop="projectid" />
      <el-table-column label="订阅" align="center" prop="topic" />
      <el-table-column label="UUID" align="center" prop="sjuuid" />
      <el-table-column label="原始数据" align="center" prop="tcdata" />
      <el-table-column label="状态" align="center" prop="isread" >
        <template #default="scope">
			{{scope.row.isread=='2'?'已入':'未入'}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="评分标准" align="center" prop="projecttypeid" /> -->
      <!-- <el-table-column label="班级" align="center" prop="classid" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="countFS(scope.row)" >计算</el-button>
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tc:tcdevicedata:edit']">修改</el-button> -->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tc:tcdevicedata:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体测原始数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tcdevicedataRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="clientid">
          <el-input v-model="form.clientid" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="学生" prop="studentid">
          <el-input v-model="form.studentid" placeholder="请输入学生" />
        </el-form-item>
        <el-form-item label="体测教师" prop="teacher">
          <el-input v-model="form.teacher" placeholder="请输入体测教师" />
        </el-form-item>
        <el-form-item label="学校ID" prop="deptid">
          <el-input v-model="form.deptid" placeholder="请输入学校ID" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectid">
          <el-input v-model="form.projectid" placeholder="请输入体测项目ID" />
        </el-form-item>
        <el-form-item label="订阅" prop="topic">
          <el-input v-model="form.topic" placeholder="请输入订阅" />
        </el-form-item>
        <el-form-item label="读取状态" prop="isread">
          <el-input v-model="form.isread" placeholder="请输入1未读 2已读" />
        </el-form-item>
        <el-form-item label="证件号" prop="sfz">
          <el-input v-model="form.sfz" placeholder="请输入唯一证件号" />
        </el-form-item>
        <el-form-item label="体测项目" prop="projecttypeid">
          <el-input v-model="form.projecttypeid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="班级ID" prop="classid">
          <el-input v-model="form.classid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="年级ID" prop="classnj">
          <el-input v-model="form.classnj" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
	
    <!-- 根据规则计算分数 -->
	<el-dialog :title="title2" v-model="open2" width="500px" append-to-body>
	      <el-form ref="jcsjRef2" :model="form"  label-width="120px">
			<el-form-item label="体测评分标准" prop="jcsjXmid">
				<el-select filterable v-model="form.projecttypeid" placeholder="体测评分标准" :disabled="true">
					<el-option
					  v-for="d in tctypeList"
					  :key="d.id"
					  :label="d.xmflTitle"
					  :value="d.id"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="体测项目" prop="jcsjXmid">
				<el-select filterable v-model="form.projectid" placeholder="体测项目"  :disabled="true">
					<el-option
					  v-for="d in projectList"
					  :key="d.id"
					  :label="d.xmMc"
					  :value="d.id"
					/>
				</el-select>
			</el-form-item>
	      </el-form>
	      <template #footer>
	        <div class="dialog-footer">
	          <el-button type="primary" @click="submitForm2">确 定</el-button>
	          <el-button @click="cancel2">取 消</el-button>
	        </div>
	      </template>
	    </el-dialog>
  </div>
</template>

<script setup name="Tcdevicedata">
import { checkPermi, checkRole } from "@/utils/permission"; // 1. 导入权限判断函数
import { listTcdevicedata, getTcdevicedata, delTcdevicedata, addTcdevicedata, updateTcdevicedata,count } from "@/api/tc/tcdevicedata";
import { listTcsj} from "@/api/tc/tcsj";
import { listTctype} from "@/api/tc/tctype";
import { listProject} from "@/api/tc/project";
import useUserStore from '@/store/modules/user'
var userStore=useUserStore();
const { proxy } = getCurrentInstance();
const {sys_user_sex,tc_nj } = proxy.useDict('sys_user_sex','tc_nj');


const tcdevicedataList = ref([]);
const tctypeList = ref([]);
const projectList = ref([]);

const open = ref(false);
const open2 = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title2 = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    clientid: null,
    studentid: null,
    teacher: null,
    deptid: null,
    projectid: null,
    topic: null,
    tcdata: null,
    isread: null,
    sfz: null,
    projecttypeid: null,
    classid: null,
    classnj: null
  },
  rules: {
    type: [
      { required: true, message: "A体测 B娱乐不能为空", trigger: "change" }
    ],
    clientid: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    tcdata: [
      { required: true, message: "体测原始数据不能为空", trigger: "blur" }
    ],
    isread: [
      { required: true, message: "1未读 2已读不能为空", trigger: "blur" }
    ],
    sfz: [
      { required: true, message: "唯一证件号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
const tmpGetPar=ref({"pageNum": 1,"pageSize": 500,"bjXxid" : '-1'});
const tmpProPar={pageNum: 1,pageSize: 999};

 onMounted(() => {
      // console.log('组件已经挂载')
	getList();
	getTcTypeList();
	getProjectList();
	
 })

 /** 查询评分标准列表 */
 function getTcTypeList() {
   loading.value = true;
   listTctype(tmpGetPar).then(response => {
     tctypeList.value = response.rows;
     loading.value = false;
 	getProjectList();
   });
 }
 /** 查询体测项目列表 */
 function getProjectList() {
   loading.value = true;
   listProject(tmpProPar).then(response => {
     projectList.value = response.rows;
     loading.value = false;
   });
 }
 
/** 查询体测原始数据列表 */
function getList() {
  loading.value = true;
  
	if(checkRole(['admin','common'])){
	  
	}else{
		queryParams.value.deptid=userStore.user.deptId;
	}
  // queryParams.value.deptid=userStore.user.deptId;
  listTcdevicedata(queryParams.value).then(response => {
    tcdevicedataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 计算取消按钮
function cancel2() {
  open2.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    clientid: null,
    studentid: null,
    teacher: null,
    deptid: null,
    projectid: null,
    topic: null,
    tcdata: null,
    isread: null,
    sfz: null,
    createTime: null,
    updateTime: null,
    projecttypeid: null,
    classid: null,
    classnj: null
  };
  proxy.resetForm("tcdevicedataRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加体测原始数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTcdevicedata(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改体测原始数据";
  });
}

/** 计算按钮操作 */
function countFS(row) {
  reset();
  // console.log(row)
  // form.value.projecttypeid=row.projecttypeid;
  // form.value.projectid=row.projectid;
  form.value=row;
  // const _id = row.id || ids.value
	open2.value = true;
	title2.value = "计算分数";
}
/** 计算提交按钮 */
function submitForm2() {
	let t={"pageNum": 1,"pageSize": 10,"sjData" : form.value.id};
	listTcsj(t).then(response => {
	  if(response.rows.length>0){
          proxy.$modal.alertWarning("此原始数据已计算过分数并存入体测数据表!");
	  }else{
		count(form.value).then(response => {
		  proxy.$modal.msgSuccess("计算成功");
		  open2.value = false;
		});
	  }
	});
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["tcdevicedataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTcdevicedata(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTcdevicedata(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除体测原始数据编号为"' + _ids + '"的数据项？').then(function() {
    return delTcdevicedata(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tc/tcdevicedata/export', {
    ...queryParams.value
  }, `tcdevicedata_${new Date().getTime()}.xlsx`)
}

</script>
